﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Bll;
using Runda.CMS.Common.Utility;
using Runda.CMS.Common.Entity;

namespace Runda.CMS.Portal.SyncManage
{
    public partial class CustomerInfo : BasePage
    {
        private CtrtCustomerBll customerBll = new CtrtCustomerBll();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) {
                initForm();
            }
        }

        private void initForm()
        {
            if (Request.QueryString["CustomerID"] != null && Request.QueryString["CustomerID"] != "")
            { 
                CtrtCustomerEntity customerEntity = customerBll.Get<CtrtCustomerEntity>(new Guid(Request.QueryString["CustomerID"].ToString()));
                lblCustomerCode.Text = customerEntity.CustomerCode;
                txtCustomerAddress.Text= customerEntity.CustomerAddress;
                txtContactor.Text = customerEntity.Contactor;
                txtContactphone.Text =   customerEntity.Contactphone;
                txtCustomerID.Value = customerEntity.CustomerID.ToString();
                txtCustomerName.Text = customerEntity.CustomerName;
                salesorGUID.Value = customerEntity.Salesman;
                if (customerEntity.SalesModelType != null)
                {
                    SalesModelType.SelectedValue = customerEntity.SalesModelType;
                }

                if (customerEntity.SalesType != null)
                {
                    SalesType.SelectedValue = customerEntity.SalesType;
                }

                if (customerEntity.Salesman != null && customerEntity.Salesman.ToString() != "")
                {
                    salesorName.Text = new SysUserBll().Get<SysUserEntity>(new Guid(customerEntity.Salesman)).UserName;
                }
                WsmCustomerExtensionEntity customerExtension = customerBll.Get<WsmCustomerExtensionEntity>(new Guid(Request.QueryString["CustomerID"].ToString()));
                if (customerExtension != null) {
                    BelongToSalor.Text = customerExtension.OfficerTelephone;
                    OfficerEmail.Text = customerExtension.OfficerEmail;
                    DepartmentOfficer.Text = customerExtension.DepartmentOfficer;
                    HospitalLevel.Text = customerExtension.HospitalLevel;
                    HospitalNickName.Text = customerExtension.HospitalNickName;
                    BelongToTeam.Text = customerExtension.BelongToTeam;
                    ServiceLevel.SelectedValue = customerExtension.ServiceLevel;
                    CustomerArea.Text = customerExtension.CustomerArea;
                }
                BindGrid();
            }         
        }

        protected void lnkbtnSaveAndClose_Click(object sender, EventArgs e) 
        {
            save();
            ClientScript.RegisterStartupScript(this.GetType(), "save", "alert('保存成功');window.close();", true);       
        }

        private void save()
        {
            CtrtCustomerEntity ctrtCustomerEntity;
            if (txtCustomerID.Value.Trim() !="")
            {
                ctrtCustomerEntity = customerBll.Get<CtrtCustomerEntity>(new Guid(txtCustomerID.Value.Trim()));
            }
            else
            {
                ctrtCustomerEntity = new CtrtCustomerEntity();                               
            }
            ctrtCustomerEntity.Contactor = txtContactor.Text;
            ctrtCustomerEntity.Contactphone = txtContactphone.Text;
            ctrtCustomerEntity.CustomerAddress = txtCustomerAddress.Text;
            ctrtCustomerEntity.CustomerName = txtCustomerName.Text;
            ctrtCustomerEntity.Salesman = salesorGUID.Value;
            if (SalesModelType.SelectedValue.ToString() != "")
            {
                ctrtCustomerEntity.SalesModelType = SalesModelType.SelectedValue;
            }
            if (SalesType.SelectedValue.ToString() != "")
            {
                ctrtCustomerEntity.SalesType = SalesType.SelectedValue;
            }

            customerBll.InsertOrUpdate(ctrtCustomerEntity);


            customerBll.Delete<WsmCustomerExtensionEntity>(" and CustomerID= '"+txtCustomerID.Value.Trim()+"'");
            WsmCustomerExtensionEntity entity = new WsmCustomerExtensionEntity();
            entity.CustomerID = new Guid(txtCustomerID.Value.Trim());
            entity.BelongToTeam = BelongToTeam.Text.Trim();
            entity.DepartmentOfficer = DepartmentOfficer.Text.Trim();
            entity.HospitalLevel = HospitalLevel.Text.Trim();
            entity.HospitalNickName = HospitalNickName.Text.Trim();
            entity.OfficerEmail = OfficerEmail.Text.Trim();
            entity.BelongToSalor = BelongToSalor.Text.Trim();
            entity.ServiceLevel = ServiceLevel.SelectedValue;
            entity.CustomerArea = CustomerArea.Text.Trim();
            customerBll.Insert(entity);
        }

        protected void gridContractItems_ItemCommand(object source, Telerik.WebControls.GridCommandEventArgs e)
        {
            if (e.CommandName.ToLower() == "delete")
            { 
                ContractBll bll = new ContractBll();
                Guid UserID = new Guid(gridUsers.MasterTableView.DataKeyValues[e.Item.ItemIndex]["UserID"].ToString());
                  bll.Delete<CtrtCustomerUserEntity>(" and CustomerID = '" + Request.QueryString["CustomerID"] + "' and UserID = '" + UserID + "'");
                  BindGrid();
            }
        }

        protected void btnRefersh_Click(object sender, EventArgs e)
        {
            BindGrid();
        }

        private void BindGrid()
        {
            ContractBll bll = new ContractBll();
            List<SysUserEntity> list = bll.GetAll<SysUserEntity>(" and UserID in (select UserID from Cms_Ctrt_CustomerUser where CustomerID='" + txtCustomerID.Value + "')");

            gridUsers.DataSource = list;
            gridUsers.DataBind();
        }
    }
}
